/**
 * 生成全局唯一的表单项ID
 * @param options 配置对象，包含各种列表
 * @returns {number} 新的唯一ID
 */
export const nextIdGlobal = (options) => {
  const { formConf, drawingList, dataSourceList, methodList, objectList } = options
  let idGlobal = 100

  const findMaxId = (list) => {
    if (!Array.isArray(list)) return 0
    let max = 0
    list.forEach(item => {
      if (item.__config__) {
        const { formId, children } = item.__config__
        if (formId > max) max = formId
        if (Array.isArray(children)) {
          const childMax = findMaxId(children)
          if (childMax > max) max = childMax
        }
      } else if (typeof item === 'object') {
        const id = item.id || 0
        if (id > max) max = id
      }
    })
    return max
  }

  const lists = [
    drawingList || [],
    dataSourceList || [],
    methodList || [],
    objectList || []
  ]

  lists.forEach(list => {
    const max = findMaxId(list)
    if (max > idGlobal) idGlobal = max
  })

  return ++idGlobal
}

export default {
  nextIdGlobal
}
